package com.yyfax.pay.config.data.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import com.yyfax.framework.dao.impl.BaseDaoImpl;
import com.yyfax.framework.exception.YYSqlException;
import com.yyfax.framework.sql.SQLFactory;
import com.yyfax.pay.config.data.dao.MerchantPaymentDao;
import com.yyfax.pay.config.model.po.MerchantPaymentPO;

@Repository
public class MerchantPaymentDaoImpl extends BaseDaoImpl<MerchantPaymentPO, Integer> implements MerchantPaymentDao {

	// 这里写死,将来可以考虑注入
	private static final String TABLE_NAME = "merchant_payment";

	private static final String SQL_INSERT = "insert";

	private static final String SQL_UPDATE = "update";

	private static final String SQL_SELECT_FOR_LIST = "selectForList";
	/** 根据渠道ID查询 */
	private static final String SQL_SELECT_BY_ID = "selectById";
	/** 根据四要素查询 */
	private static final String SQL_SELECT_MERCHANTPAYMENT = "selectMerchantPayment";
	/** 根据四要素查询 */
	private static final String SQL_SELECT_MERCHANTPAYMENTID = "selectMerchantPaymentId";
	/** 根据三要素查询 DefaultByMerchantCode */
	private static final String SQL_SELECT_DEFAULTBYMERCHANTCODE = "selectDefultMerchantPayment";
	
	private static final String SQL_SELECT_LIST_BY_MERCHANT_AND_PAYTYPE = "selectListByMerchantAndPayType";

	public MerchantPaymentDaoImpl() {
		super(TABLE_NAME);
	}

	@Autowired
	private SQLFactory sqlFactory;

	@Override
	public int insert(MerchantPaymentPO config) throws YYSqlException {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_INSERT);

		return namedParameterJdbcTemplate.insertForPK(sql, new BeanPropertySqlParameterSource(config));
	}

	@Override
	public MerchantPaymentPO selectById(int id) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_BY_ID);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("Id", id);

		return namedParameterJdbcTemplate.queryForObject(sql, params, new BeanPropertyRowMapper<MerchantPaymentPO>(MerchantPaymentPO.class));
	}

	@Override
	public List<MerchantPaymentPO> selectForList() throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_FOR_LIST);
		return namedParameterJdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<MerchantPaymentPO>(MerchantPaymentPO.class));
	}

	@Override
	public MerchantPaymentPO selectMerchantPayment(String merchantCode, String paymentCode, String payType, String payMode) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_MERCHANTPAYMENT);

		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("paymentCode", paymentCode);
		params.addValue("payType", payType);
		params.addValue("payMode", payMode);

		return namedParameterJdbcTemplate.queryForObject(sql.toString(), params,
				new BeanPropertyRowMapper<MerchantPaymentPO>(MerchantPaymentPO.class));
	}

	@Override
	public int update(MerchantPaymentPO config) throws YYSqlException {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_UPDATE);
		return namedParameterJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(config));
	}

	@Override
	public MerchantPaymentPO selectMerchantPaymentId(String payMerchantId, String paymentCode, String payType, String payMode) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_MERCHANTPAYMENTID);
		MapSqlParameterSource params = new MapSqlParameterSource();

		params.addValue("payMerchantId", payMerchantId);
		params.addValue("paymentCode", paymentCode);
		params.addValue("payType", payType);
		params.addValue("payMode", payMode);

		return namedParameterJdbcTemplate.queryForObject(sql.toString(), params,
				new BeanPropertyRowMapper<MerchantPaymentPO>(MerchantPaymentPO.class));
	}

	@Override
	public MerchantPaymentPO selectDefaultByMerchantCode(String merchantCode, String payType, String payMode,String apiType) throws Exception {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_DEFAULTBYMERCHANTCODE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("payType", payType);
		params.addValue("payMode", payMode);
		
		
		if (StringUtils.isNotBlank(apiType)) { // 接口模式
			sql = sqlFactory.processSql(sql, "#condition#", " and api_type = :apiType ");
			params.addValue("apiType", apiType);
		}else {
			sql = sqlFactory.processSql(sql, "#condition#", "");
		}

		return namedParameterJdbcTemplate.queryForObject(sql.toString(), params,
				new BeanPropertyRowMapper<MerchantPaymentPO>(MerchantPaymentPO.class));
	}

	@Override
	public List<MerchantPaymentPO> selectListByMerchantAndPayType(String merchantCode, String payType) {
		String sql = sqlFactory.getSQL(TABLE_NAME, SQL_SELECT_LIST_BY_MERCHANT_AND_PAYTYPE);
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("merchantCode", merchantCode);
		params.addValue("payType", payType);
		
		return namedParameterJdbcTemplate.query(sql, params, new BeanPropertyRowMapper<MerchantPaymentPO>(poClass));
	}
	
}
